package com.carsystems.controller;

import com.carsystems.mapper.OperatingCompanyMapper;
import com.carsystems.pojo.OperatingCompany;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
public class OperatingCompanyController {
    @Autowired
    OperatingCompanyMapper operatingCompanyMapper;

    @GetMapping("/getList")
    public OperatingCompany getList(int company_id){
        OperatingCompany operatingCompany = operatingCompanyMapper.getList(company_id);
        System.out.println(operatingCompany.getUser().get(1));
        return operatingCompany;
    }

    //查看全部
    @GetMapping("/querCompanyList")
    public List<OperatingCompany> querCompanyList() {
        List<OperatingCompany> operatingCompanyList = operatingCompanyMapper.querCompanyList();
        return operatingCompanyList;
    }

    //按照company_id查询
    @GetMapping("/querCompanyByPrimaryKey")
    public OperatingCompany querCompanyByPrimaryKey(int company_id) {
        OperatingCompany company = operatingCompanyMapper.querCompanyByPrimaryKey(company_id);
            return company;
    }

    //增加一个公司
    @ResponseBody
    @PostMapping("/addCompany")
    public String addCompany(@RequestBody OperatingCompany company) {
        operatingCompanyMapper.addCompany(company);
        if (company != null) {
            return "success";
        } else {
            return "fail";
        }
    }

    //修改公司信息
    @ResponseBody
    @PostMapping("/updateCompany")
    public String updateCompany(@RequestBody OperatingCompany company){
        int row = operatingCompanyMapper.updateCompany(company);
        operatingCompanyMapper.querCompanyByPrimaryKey(company.getCompany_id());
        if (row==1){
            return "success";
        }else {
            return "fail";
        }
    }

    //删除公司
    @ResponseBody
    @GetMapping("/daleteCompany")
    public String daleteCompany(OperatingCompany company){
        int row = operatingCompanyMapper.daleteCompany(company.getCompany_id());
        if (row == 1){
            return "success";
        }else {
            return "fail";
        }
    }

    //模糊查询公司列表
    @RequestMapping("/findCompanyName")
public List<OperatingCompany> findCompanyName(String name){
        List<OperatingCompany> list = operatingCompanyMapper.findCompanyName("%" + name + "%");
        return list;
}

//按类型查询公司列表
    @RequestMapping("/findCompanyType")
    public List<OperatingCompany> findCompanyType(String Typename){
        List<OperatingCompany> list = operatingCompanyMapper.findCompanyType("%" + Typename + "%");
        return list;
    }
}
